package com.glory.dgcpcpda.activity.mfg;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.DefectCodeEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EqpModificationActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.tvCircuit)
    TextView tvCircuit;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvAPSEqp)
    TextView tvAPSEqp;
    @BindView(R.id.etModificationEqp)
    FreeUI_ClearEditText etModificationEqp;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_modification;
    }

    @Override
    protected void initView() {
        tvBaseSubmit.setVisibility(View.GONE);
        tvTitle.setText("派工设备改机");
        setMainLayout(llMain);
        initEdit(etLotID, s -> LotUtils.searchLotInfo(EqpModificationActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvCircuit.setText(response.getPRODUCTMODEL());
                tvCustomCode.setText(response.getLOT().getCUSTOMERCODE());
                tvPartName.setText(response.getCHIPNAME());
                tvPackageType.setText(response.getLOT().getMAINMATTYPE());
                tvAPSEqp.setText(response.getLOT().getUDF().getAPSPLANTESTEQP());
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t,"LOTQUERY");
            }
        }));
    }

    @Override
    protected void initData() {

    }


    @OnClick(R.id.tvSubmit)
    public void onViewClicked() {
        if (actionControl("Submit33"))
            return;

        if (getText(etModificationEqp).equals("") || getText(etLotID).equals("")){
            showToast("改机设备或批次号不能为空",false);
            return;
        }

        showSubmitDialog(() -> {
            FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                    (new TypeToken<BaseResponseBean<JSONObject>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                        showConfirmDialog(EqpModificationActivity.this,"改机成功！");
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<JSONObject>> response) {
                    super.onError(response);
                    loadError(response.getException(),"CHANGEAPSEQP");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("LOTID",getText(etLotID));
            map.addProperty("EQUIPMENTID",getText(etModificationEqp));
            NetUtils.requestPostNet("CPC.CHANGEAPSEQP","CHANGEAPSEQP",this,map,callBack);
        });


    }
}